import Koa from 'koa'
import koaBody from 'koa-body'
// @ts-ignore
import cors from '@koa/cors'
import koaValidate from 'koa-validate'
import config from './config'
import { auth } from './middlewares/auth'
import { resFormatter } from './middlewares/resFormatter'
import './models'
import { autoRegistAllRouter } from './routes'
import './test/index'
import './utils/ValidatorAddError'

const app = new Koa()
const port = config.port

// 跨域中间件
app.use(cors())

// 解析body中间件
app.use(koaBody())

// 注入参数校验函数
koaValidate(app)

// 登录校验中间件
app.use(auth)

// 返回格式化以及错误处理中间件
app.use(resFormatter)

// 注册路由
autoRegistAllRouter(app)

app.listen(port, () => {
    console.log(`parking-node running at http://localhost:${port}`)
})
